﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// resource 资源加载
/// </summary>
public class ResourceLoader : IAssetsLoader
{
    public void Load<T>(AssetsCatalog assetsCatalog, string assetKey, Action<T> onAssetLoad) where T : UnityEngine.Object
    {
        var assetItem = assetsCatalog.GetAssetItemByKey(assetKey);
        if (assetItem == null) return;
        var request = Resources.LoadAsync<T>(assetItem.assetPath);
        request.completed += operation =>
        {
            if (operation.isDone)
            {
                onAssetLoad?.Invoke(request.asset as T);
            }
            else
            {
                Debug.LogError($"asset load failed,assetCatalog{assetsCatalog.name},assetKey{assetKey}");
            }
        };
    }
}
